package com.juc.completableFuture;

import java.util.concurrent.CompletableFuture;


public class CompletableFutureDemo4 {

	public static void main(String[] args) {
		System.out.println(CompletableFuture.supplyAsync(() ->{
			return 1;
		}).thenApply(f->{
			System.out.println("---------1");
			int a = 10/0;
			return f +2;
		}).thenApply(f-> {
			System.out.println("---------2");
			return f +3;
		}).thenApply(f-> {
			System.out.println("---------3");
			return f +4;
		}).whenComplete((v,e) ->{
			if (e== null) {
				System.out.println("--- result ---" + v); 
			}
		}).exceptionally(e->{
			e.printStackTrace();
			return null;
		}).join());
		
		System.out.println("***************************");
		System.out.println(CompletableFuture.supplyAsync(()->{
			return 1;
		}).handle((f,e)->{
			System.out.println("---------1");
			int a = 10/0;
			return f +2;
		}).handle((f,e)->{
			System.out.println("---------2");
			return f +3;
		}).handle((f,e)->{
			System.out.println("---------3");
			return f +4;
		}).whenComplete((v,e) ->{
			if (e== null) {
				System.out.println("--- result ---" + v); 
			}
		}).exceptionally(e->{
			e.printStackTrace();
			return null;
		}).join());
	}
}
